home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue24 / survive / UALLO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-06-11  |  5.0 KB  |  219 lines

  1. unit uAllo;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, uBase;
  7.  
  8. type
  9.   TAmountsList = class(TList)
  10.       { This is a generic class that manages a list of dollar amounts (stored
  11.         as LongInts). }
  12.     protected
  13.       function GetItem(aIndex: Integer): LongInt;
  14.       procedure SetItem(aIndex: Integer; aValue: LongInt);
  15.     public
  16.       procedure Add(aValue: LongInt);
  17.       procedure Assign(aSource: TAmountsList);
  18.       procedure ZeroOut;
  19.       property Items[aIndex: Integer]: LongInt read GetItem write SetItem; default;
  20.     end;
  21.  
  22.   TCredit = class
  23.     public
  24.       CreditNo: LongInt;
  25.       Amount,
  26.       AmountRemaining: LongInt;
  27.       PaymentByMethod: TAmountsList;
  28.       constructor Create(aNumMethods: Integer);
  29.       destructor Destroy; override;
  30.     end;
  31.  
  32.   TCreditList = class(TList)
  33.     protected
  34.       function GetItem(aIndex: Integer): TCredit;
  35.     public
  36.       procedure Add(aCreditNo: LongInt; aAmount: LongInt);
  37.       procedure Delete(aCreditNo: LongInt);
  38.       property Items[aIndex: Integer]: TCredit read GetItem; default;
  39.     end;
  40.  
  41.   TAllocationInfo = class(TPersistent)
  42.     protected
  43.       function GetMethodCount: Integer;
  44.       function GetMethodName(aIndex: Integer): string;
  45.     public
  46.       MethodAmounts: TAmountsList;
  47.       MethodAmountsRemaining: TAmountsList;
  48.       Credits: TCreditList;
  49.       TotalPaymentRemaining: LongInt;
  50.  
  51.       constructor Create;
  52.       destructor Destroy; override;
  53.       procedure Clear;
  54.       procedure ComputeTotalByCredit(aCreditNo: LongInt);
  55.       procedure ComputeTotalByMethod(aMethodNo: Integer);
  56.  
  57.       property MethodCount: Integer
  58.         read GetMethodCount;
  59.       property MethodName[aIndex: Integer]: string
  60.         read GetMethodName;
  61.     end;
  62.  
  63. implementation
  64.  
  65. uses
  66.   dmData;
  67.  
  68. { TAmountsList }
  69.  
  70. function TAmountsList.GetItem(aIndex: Integer): LongInt;
  71. begin
  72.   Result := 0;
  73.   if aIndex < Count then
  74.     Result := LongInt(inherited Items[aIndex]);
  75. end;
  76.  
  77. procedure TAmountsList.SetItem(aIndex: Integer; aValue: LongInt);
  78. begin
  79.   inherited Items[aIndex] := Pointer(aValue);
  80. end;
  81.  
  82. procedure TAmountsList.Add(aValue: LongInt);
  83. begin
  84.   inherited Add(Pointer(aValue));
  85. end;
  86.  
  87. procedure TAmountsList.Assign(aSource: TAmountsList);
  88. var
  89.   I: Integer;
  90. begin
  91.   Clear;
  92.   for I := 0 to aSource.Count - 1 do
  93.     Add(aSource.Items[I]);
  94. end;
  95.  
  96. procedure TAmountsList.ZeroOut;
  97. var
  98.   I: Integer;
  99. begin
  100.   for I := 0 to Count - 1 do
  101.     Items[I] := 0;
  102. end;
  103.  
  104. { TCredit }
  105.  
  106. constructor TCredit.Create(aNumMethods: Integer);
  107. begin
  108.   inherited Create;
  109.   PaymentByMethod := TAmountsList.Create;
  110.   while aNumMethods > 0 do begin
  111.     PaymentByMethod.Add(0);
  112.     Dec(aNumMethods);
  113.   end;
  114. end;
  115.  
  116. destructor TCredit.Destroy;
  117. begin
  118.   PaymentByMethod.Free;
  119.   inherited Destroy;
  120. end;
  121.  
  122. { TCreditList }
  123.  
  124. procedure TCreditList.Add(aCreditNo: LongInt; aAmount: LongInt);
  125. var
  126.   Credit: TCredit;
  127. begin
  128.   Credit := TCredit.Create(dmDataModule.PaymentMethodsList.Count);
  129.   Credit.CreditNo := aCreditNo;
  130.   Credit.Amount := aAmount;
  131.   Credit.AmountRemaining := aAmount;
  132.   inherited Add(Credit);
  133. end;
  134.  
  135. procedure TCreditList.Delete(aCreditNo: LongInt);
  136. var
  137.   I: Integer;
  138. begin
  139.   for I := 0 to Count - 1 do
  140.     if Items[I].CreditNo = aCreditNo then begin
  141.       inherited Delete(I);
  142.       Break;
  143.     end;
  144. end;
  145.  
  146. function TCreditList.GetItem(aIndex: Integer): TCredit;
  147. begin
  148.   Result := nil;
  149.   if aIndex < Count then
  150.     Result := TCredit(inherited Items[aIndex]);
  151. end;
  152.  
  153. { TAllocationInfo }
  154.  
  155. constructor TAllocationInfo.Create;
  156. var
  157.   I: Integer;
  158. begin
  159.   inherited Create;
  160.   MethodAmounts := TAmountsList.Create;
  161.   MethodAmountsRemaining := TAmountsList.Create;
  162.   Credits := TCreditList.Create;
  163.  
  164.   for I := 1 to MethodCount do begin
  165.     MethodAmounts.Add(0);
  166.     MethodAmountsRemaining.Add(0);
  167.   end;
  168. end;
  169.  
  170. destructor TAllocationInfo.Destroy;
  171. begin
  172.   MethodAmounts.Free;
  173.   MethodAmountsRemaining.Free;
  174.   Credits.Free;
  175.   inherited Destroy;
  176. end;
  177.  
  178. procedure TAllocationInfo.Clear;
  179. var
  180.   J: Integer;
  181. begin
  182.   {!! markers }
  183.   for J := 0 to MethodCount - 1 do begin
  184.     MethodAmounts[J] := 0;
  185.     MethodAmountsRemaining[J] := 0;
  186.   end;
  187. end;
  188.  
  189. procedure TAllocationInfo.ComputeTotalByCredit(aCreditNo: LongInt);
  190. begin
  191.   {!!}
  192. end;
  193.  
  194. procedure TAllocationInfo.ComputeTotalByMethod(aMethodNo: Integer);
  195. var
  196.   I: Integer;
  197. begin
  198.   MethodAmountsRemaining[aMethodNo] := MethodAmounts[aMethodNo];
  199.   for I := 0 to Credits.Count - 1 do
  200.     MethodAmountsRemaining[aMethodNo] := MethodAmountsRemaining[aMethodNo] -
  201.                                          Credits[I].PaymentByMethod[aMethodNo];
  202.  
  203.   TotalPaymentRemaining := 0;
  204.   for I := 0 to MethodCount - 1 do
  205.     Inc(TotalPaymentRemaining, MethodAmountsRemaining[I]);
  206. end;
  207.  
  208. function TAllocationInfo.GetMethodCount: Integer;
  209. begin
  210.   Result := dmDataModule.PaymentMethodsList.Count;
  211. end;
  212.  
  213. function TAllocationInfo.GetMethodName(aIndex: Integer): string;
  214. begin
  215.   Result := dmDataModule.PaymentMethodsList[aIndex];
  216. end;
  217.  
  218. end.
  219.